package com.shankungfu.main;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shankungfu.GlobleConstant;
import com.shankungfu.MainApplication;
import com.shankungfu.R;
import com.shankungfu.bean.BaseBean;
import com.shankungfu.util.MyAsyncHttpResponseHandler;
import com.shankungfu.util.ToastUtil;
import com.shankungfu.util.WebService;
import com.shankungfu.view.CustomDialog;
import com.shankungfu.view.CustomDialog.Builder;
import com.shankungfu.view.HorizontalListView;

public class SecondCommentActivity extends Activity implements OnClickListener{
	private static final int DATA_SUCCESS = 1;
	
	private ImageView head_reply;
	private ImageView head_goback;
	private ImageView iv;
	private TextView detail_title;
	private TextView author;
	private TextView date;
	private TextView content;
	private ImageView user_icon;
	private HorizontalListView pic_listview;
	private EditText second_comment_et;
	private String reply_id = "0";
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case DATA_SUCCESS:
				BaseBean baseBean = (BaseBean) msg.obj;
				if(baseBean.getResult().equals("0")){
					CustomDialog.Builder builder = new Builder(SecondCommentActivity.this);
					builder.setMessage("回复成功!");
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int arg1) {
							dialog.dismiss();
							onBackPressed();
						}
					});
					builder.create().show();
				} else {
					ToastUtil.showMessageText(SecondCommentActivity.this, baseBean.getMsg());
				}
				break;

			default:
				break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second_comment);
		
		init();
		
	}
	
	private void init(){
		Bundle bundle = getIntent().getBundleExtra("replyBean");
		reply_id = bundle.getString("reply_id");
		String authorStr = bundle.getString("author");
		String dateStr = bundle.getString("date");
		String contentStr = bundle.getString("content");
		String imageUrl = bundle.getString("image");
		String commentStr = bundle.getString("comment");
		
		head_reply = (ImageView) findViewById(R.id.head_refresh);
		head_goback = (ImageView) findViewById(R.id.head_goback);
		detail_title = (TextView) findViewById(R.id.detail_title);
		second_comment_et = (EditText) findViewById(R.id.second_comment_et);
		author = (TextView) findViewById(R.id.message_child_author);
		date = (TextView) findViewById(R.id.message_child_date);
		content = (TextView) findViewById(R.id.message_child_content);
		user_icon = (ImageView) findViewById(R.id.message_child_user_pic);
		pic_listview = (HorizontalListView) findViewById(R.id.pic_listview);
		
		pic_listview.setVisibility(View.GONE);


		head_reply.setImageResource(R.drawable.send_button);

		ImageLoader imageLoader = ImageLoader.getInstance();
		imageLoader.displayImage(imageUrl, user_icon, MainApplication.getOptions(), null);
		
		detail_title.setText("评论");
		author.setText(authorStr);
		date.setText(dateStr);
		content.setText(contentStr);
		if(commentStr != null){
			second_comment_et.setText("@" + commentStr + ":");
		}
		
		head_goback.setOnClickListener(this);
		head_reply.setOnClickListener(this);
	}
	
	private void sendCommentData(){
		WebService httpclient = new WebService();
		RequestParams params = new RequestParams();
		params.add("serviceId", "reply_comment");
		params.add("app_info", "shankungfu");
		params.add("reply_id", reply_id);
		params.add("comment_content", second_comment_et.getText().toString().trim());

		AsyncHttpResponseHandler responseHandler = new MyAsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				if (!TextUtils.isEmpty(content)) {
					BaseBean baseBean = JSON.parseObject(
							content, BaseBean.class);

					Message msg = new Message();
					msg.what = DATA_SUCCESS;
					msg.obj = baseBean;
					handler.sendMessage(msg);
				}
			}

			@Override
			public void onFailure(String content) {
			}
		};
		httpclient.request(GlobleConstant.ACTIVITY_REPLY_COMMENT, params,
				responseHandler);
	}

	@Override
	public void onBackPressed() {
//		super.onBackPressed();
		setResult(CommentReplyActivity.SUCCESS_CODE);
		finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.head_goback:
			onBackPressed();
			break;
		case R.id.head_refresh:
			sendCommentData();
			break;

		default:
			break;
		}
	}
}
